package cn.edu.lingnan.lsqn.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.cdahua.basic.dao.BaseDao;
import cn.edu.lingnan.lsqn.model.Column;
import cn.edu.lingnan.lsqn.model.Group;
import cn.edu.lingnan.lsqn.model.GroupColumn;

@Repository("groupDao")
public class GroupDao extends BaseDao<Group> implements IGroupDao {
	
	@Override
	public List<Group> list() {
		return list("from Group");
	}

	@Override
	public void deleteGroupUsers(int gid) {
		String hql = "delete UserGroup ug where ug.group.id = ?";
		this.updateByHql(hql, gid);
	}

	@Override
	public void addGroupColumn(Group group, Column column) {
		GroupColumn gc = this.loadGroupColumn(group.getId(), column.getId());
		if(gc!=null) return;
		gc = new GroupColumn(group, column);
		this.getSession().save(gc);
	}

	@Override
	public void deleteGroupColumn(int g_id) {
		String hql = "delete GroupColumn gc where gc.group.id = ?";
		updateByHql(hql,g_id);
	}

	@Override
	public GroupColumn loadGroupColumn(int g_id, int c_id) {
		String hql = "select gc from GroupColumn gc left join fetch gc.group g left join fetch gc.column c where g.id=? and c.id=?";
		return (GroupColumn)this.getSession().createQuery(hql)
				.setParameter(0, g_id).setParameter(1, c_id).uniqueResult();
	}

}
